Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor: checking mech and requester staking validity #37

Merged
merged 10 commits into from
Aug 26, 2024

Conversation

kupermind
Copy link
Collaborator

  • Checking mech and requester staking validity.

Comment on lines -16 to -21
interface IMechMarketplace {
/// @dev Sets mech registration status.
/// @param mech Mech address.
/// @param status True, if registered, false otherwise.
function setMechRegistrationStatus(address mech, bool status) external;
}
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No need to register mech in the marketplace anymore.

Comment on lines -23 to -24
function setMechRegistrationStatus(address mech, bool status) external;

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No need to register mechs in the marketplace now.

Comment on lines +82 to +96
enum RequestStatus {
DoesNotExist,
Requested,
Delivered
}

// Agent mech version number
string public constant VERSION = "1.1.0";
// Domain separator type hash
bytes32 public constant DOMAIN_SEPARATOR_TYPE_HASH =
keccak256("EIP712Domain(string name,string version,uint256 chainId,address verifyingContract)");
// Original domain separator value
bytes32 public immutable domainSeparator;
// Original chain Id
uint256 public immutable chainId;
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Reverted back the logic for when agent mechs process requests not via a marketplace

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

request Ids must be unique and not intersect with the Marketplace ones.

// Map of request counts for corresponding addresses
mapping(address => uint256) public mapRequestCounts;
// Map of delivery counts for corresponding addresses
mapping(address => uint256) public mapDeliveryCounts;
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Adding actual delivery counts tracking.

Comment on lines +121 to +122
// Mapping of account nonces
mapping(address => uint256) public mapNonces;
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Needed for local requests.


/// @dev Changes mech marketplace address.
/// @param newMechMarketplace New mech marketplace address.
function changeMechMarketplace(address newMechMarketplace) external onlyOperator {
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not needed anymore, we stick to the specified marketplace, otherwise things become super convoluted. If there is a need to change the marketplace, makes sense to create another mech.

/// @notice This function ultimately calls mech marketplace contract to finalize the delivery.
/// @param requestId Request id.
/// @param data Self-descriptive opaque data-blob.
function _deliver(uint256 requestId, bytes memory data) internal returns (bytes memory requestData) {
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Extracted into an internal function - such that we can separate external local delivery (without a marketplace) and delivery via a marketplace

/// @dev Registers a request without a marketplace.
/// @param data Self-descriptive opaque data-blob.
/// @return requestId Request Id.
function request(bytes memory data) external payable returns (uint256 requestId) {
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Requests are also separated into local requests and requests via a marketplace.

Comment on lines -247 to -249
// Decrease the total number of requests by this mech
mapRequestsCounts[account]--;
numTotalRequests--;
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We don't decrease the number of requests now, leave them for statistics and possible karma. The more valuable agent mech will be the one that has fewer requests than deliveries, meaning it was not asked as a priority mech a lot, but it kept delivering instead of priority mechs.

/// @param requestId Request id.
/// @param data Self-descriptive opaque data-blob.
/// @param mechServiceId Mech operator service Id.
function deliverMarketplace(uint256 requestId, bytes memory data, uint256 mechServiceId) external onlyOperator {
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

When delivering via a marketplace, the mech needs to provide its service Id in order to validate in the marketplace. Discussed with Ardian, he said it should be very easy to do. We could also figure out each specific mech's service Id, but it would take O(n) operations.

Comment on lines 167 to 172
// Minimum response time
uint256 public immutable minResponseTimeout;
// Maximum response time
uint256 public immutable maxResponseTimeout;
// Approved mech bytecode hash
bytes32 public immutable mechBytecodeHash;
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

These are becoming immutable such that the contract is not ownable.

// Maximum response time
uint256 public immutable maxResponseTimeout;
// Approved mech bytecode hash
bytes32 public immutable mechBytecodeHash;
Copy link
Collaborator Author

@kupermind kupermind Aug 23, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Specifying mech contract bytecode hash this marketplace is going to work with in order to help avoid malicious mechs.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hm, I see, this answers my question below. But this also means the marketplace is static wrt to mechs.

What exact exploit are we preventing with this?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We have elaborated on this already, changing in the next PR to deal with the staking factory verification for both mech and trader staking contracts.

Comment on lines +287 to +290
uint256 priorityMechServiceId,
uint256 responseTimeout,
address requesterStakingInstance,
uint256 requesterServiceId
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In order to check the priority mech validity and requester services validity (aka traders), their service Ids need to be supplied. Also, for the requester the corresponding service staking instance is needed.

Base automatically changed from karma to tests August 25, 2024 18:56
Base automatically changed from tests to marketplace August 25, 2024 19:00
Copy link
Contributor

@DavidMinarsch DavidMinarsch left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we please see the matching activity checkers for the mech and trader staking contracts? That will make it easier to see if this is comprehensive

/// @dev Delivers a request.
/// @param requestId Request id.
/// @param requestData Self-descriptive opaque data-blob.
function deliver(uint256 requestId, bytes memory requestData) external;
/// @param deliveryMechServiceId Mech operator service Id.
function deliverMarketplace(uint256 requestId, bytes memory requestData, uint256 deliveryMechServiceId) external;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

So a delivering mech could pass as the deliveryMechServiceId someone else's id ..

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I found below that we are applying checks ..

/// @param mechServiceId Mech operator service Id.
function checkMech(address mech, uint256 mechServiceId) public view {
// Check that the mech address corresponds to the authorized bytecode hash
bytes32 mechHash = keccak256(mech.code);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

what are we checking here exactly?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Going to be obsolete

Comment on lines -310 to -313
// Agent mech itself cannot post a request
if (mapMechRegistrations[msg.sender]) {
revert UnauthorizedAccount(msg.sender);
}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why was this dropped?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Discussed, mech marketplace is going to be ownerless, and mech registration is not needed, as all the validity is based on the staking factory verification.

if (msg.sender == mech && status) {
revert UnauthorizedAccount(msg.sender);
// Check if the mech service is staked
IStaking.StakingState state = IStaking(mechStakingInstance).getStakingState(mechServiceId);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why are we not making mechStakingInstance an argument in this method? It seems this is now scoped to a single hardcoded mechStakingInstance.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not relevant anymore as part of the ongoing PR.

/// @param _karmaProxy Karma proxy contract address.
/// @param _minResponseTimeout Min response time in sec.
/// @param _maxResponseTimeout Max response time in sec.
constructor(address _factory, address _karmaProxy, uint256 _minResponseTimeout, uint256 _maxResponseTimeout) {
/// @param _agentMechBytecodeHash Approved agent mech bytecode hash.
constructor(
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why is factor no longer needed?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The factory will be needed again, but not the mech factory :)

// Maximum response time
uint256 public immutable maxResponseTimeout;
// Approved mech bytecode hash
bytes32 public immutable mechBytecodeHash;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hm, I see, this answers my question below. But this also means the marketplace is static wrt to mechs.

What exact exploit are we preventing with this?

Comment on lines +82 to +96
enum RequestStatus {
DoesNotExist,
Requested,
Delivered
}

// Agent mech version number
string public constant VERSION = "1.1.0";
// Domain separator type hash
bytes32 public constant DOMAIN_SEPARATOR_TYPE_HASH =
keccak256("EIP712Domain(string name,string version,uint256 chainId,address verifyingContract)");
// Original domain separator value
bytes32 public immutable domainSeparator;
// Original chain Id
uint256 public immutable chainId;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why?

@kupermind kupermind merged commit e8b9380 into marketplace Aug 26, 2024
1 of 2 checks passed
@kupermind kupermind deleted the staking_checker branch August 26, 2024 15:28
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants